using System.IO;
using Abp.Auditing;
using Abp.UI;
using L._52ABP.Application.Dtos;
using Microsoft.AspNetCore.Mvc;
using JYJ.AiYard.AppFolders;
using JYJ.AiYard.DataFileObjects.DataTempCache;
using JYJ.AiYard.Database;
using Microsoft.AspNetCore.Http;
using Abp.Web.Http;
using Abp.Application.Services;
using Abp.Runtime.Session;
using JYJ.AiYard.StationManagement.YardReport;

namespace JYJ.AiYard.Controllers
{
    public class FileController : AiYardControllerBase
    {
        private readonly IAppFolder _appFolders;
        private readonly IDataTempFileCacheManager _dataTempFileCacheManager;
        private readonly IHttpService _httpService;
        private readonly IAiYardDbTableIssuedCard _aiYardDbTableIssuedCard;
        private readonly ITbrInYardApplicationService _tbrInYardApplicationService;
        protected IHttpContextAccessor _httpContext;
        public FileController(
            IAppFolder appFolders, 
            IDataTempFileCacheManager dataTempFileCacheManager
             , IHttpService httpService
             , IHttpContextAccessor httpContextAccessor
             , IAiYardDbTableIssuedCard aiYardDbTableIssuedCard
             , ITbrInYardApplicationService tbrInYardApplicationService
             )
             
        {
            _appFolders = appFolders;
            _dataTempFileCacheManager = dataTempFileCacheManager;
            _httpService = httpService;
            _httpContext = httpContextAccessor;
            _aiYardDbTableIssuedCard = aiYardDbTableIssuedCard;
            _tbrInYardApplicationService = tbrInYardApplicationService;
        }

        [DisableAuditing]
        public ActionResult DownloadFilePathFile(FileDto file)
        {
            var filePath = Path.Combine(_appFolders.TempFileDownloadFolder, file.FileToken);
            if (!System.IO.File.Exists(filePath))
            {
                throw new UserFriendlyException(L("RequestedFileDoesNotExists"));
            }

            var fileBytes = System.IO.File.ReadAllBytes(filePath);
            System.IO.File.Delete(filePath);
            return File(fileBytes, file.FileType, file.FileName);
        }


        [DisableAuditing]
        public ActionResult DownloadTempFile(FileDto file)
        {

         var fileBytes=   _dataTempFileCacheManager.GetFile(file.FileToken);

         if (fileBytes==null)
         {
             return NotFound("未找到需要下载的文件");
         }

         return File(fileBytes, file.FileType, file.FileName);



        }

        [DisableAuditing]
        public object GetYardChart(int StationId, string iType, long UserId)
        {
            string host = _httpContext.HttpContext.Request.Host.Host.Replace("www", "pay").Replace("aiweb", "pay").Replace("webx", "pay").Replace("web.", "minipay.").Replace("yun4.", "pay.").Replace("p.", "t.");
#if DEBUG
            host = "pay.yuhaozhixing.com";
#endif
            
            string NoBAccount = (_aiYardDbTableIssuedCard.isBAccountQuery(UserId) ? "1" : "0");
            string Url = (host.Contains("minipay.") ? "https://" : "http://") + host + ":1888/api/v1.0/YardChart/GetChart?iType=" + iType + "&StationId=" + StationId + "&NoBAccount=" + NoBAccount;

            return _httpService.Get(Url);
        }



    }
}


